<%@ include file="/common.jsp"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<title>酱出名门人事工资管理平台</title>
	</head>
	<body>
		<div class="main">
			<%@include file="/head.jsp"%>
			<div class="nav">
				<ul>
					<li>
						<a href="managerAction.action">操作员管理</a>
					</li>
					<li class="nav_pic"></li>

				</ul>
			</div>
			<div class="center">
				<div align="left"
					style="padding: 0; margin-left: 20px; margin-top: 10px">
					<a id="btn_manager_add" href="managerAction!jumpToAdd.action">添加管理员</a>
				</div>
				<div>
					<table id="manager_table" style="width: 984px; padding: 0; margin: 10px 20px"
						cellspacing="0">
						<thead>
							<th scope="col">
								序号
							</th>
							<th scope="col">
								员工编号
							</th>
							<th scope="col">
								姓名
							</th>							
							<th scope="col">
								登录名
							</th>
							<th scope="col">
								角色
							</th>
							<th scope="col">
								所辖区域
							</th>
							<th scope="col">
								操作
							</th>
						</thead>
						<tbody>
						</tbody>
					</table>
				</div>
				<div style="float:left;margin-left:22px; width: 984px">  
					<div style='float: left; line-height: 28px;vertical-align: middle'>
						<span id="page_info"></span>
                    	<a id="page_first" herf="#">首页</a> 
                     	<a id="page_prev" herf="#">上一页</a> 
                        <a id="page_next" herf="#">下一页</a> 
                        <a id="page_last" herf="#">尾页</a>
                        <input id="page_num" type="text" class="text ui-widget-content ui-corner-all" style="height:24px;line-height:24px;width:20px;padding: 0px">
                        <a id="page_goto" herf="#">转</a> 
					    </div>
					</div>
			</div>
			<div class="clear"></div>
			<div class="footer">
				酱出名门人事工资管理平台 版权所有
			</div>
		</div>
	</body>
	<script type="text/javascript">
	$(function() {

		$("#btn_manager_add").button();
        $("#page_first").button();
        $("#page_prev").button();
        $("#page_next").button();
        $("#page_last").button();
        $("#page_goto").button();

        if (pageObjRef.startIndex == "") {
        	pageObjRef.startIndex = 0;
        } 

        findByPage(pageObjRef.startIndex);
	});

	function findByPage(startIndex) {


		pageObjRef.startIndex = startIndex;

		$.ajax( {
			type : "POST",
			url : "managerAction!findManagerByPage.action",
			data : "paginationJson=" + JSON.stringify(pageObjRef)
					+ "&timestamp=" + new Date().getTime(),
			success : function(msg) {
				findByPageCallback(msg);
			},
			error : function(textStatus) {
				alert("网络异常请稍后重试!");
			}
		});
	}


	
    function findByPageCallback(msg) {
        switch (msg.status.code) {
        case "SUCCES" :
        	showManagers(msg.datas);
            break;
        case "FAIL" :
        	alert(msg.status.message);
            break;
        case "EMPTY" :
        	alert(msg.status.message);
            break;    
        }
    }

	function jumpToUpdateManager(employeeId) {
		window.location.href="managerAction!jumpToUpdateManager.action?employeeId=" + employeeId + "&startIndex=" + pageObjRef.startIndex;
	}
	
	function showManagers(datas) {
		$("#manager_table tbody tr").remove();
		var items = datas.items;

		for (var i = 0; i < items.length; i++) {
			var tr = $("<tr></tr>").attr("id", "tr_" + items[i].employeeId);
			var td = "";
			if (i % 2 == 0) {
				tr.append($("<th scope='row' class='spec'></th>").html(i+1));
				td = "<td></td>";
			} else {
				tr.append($("<th scope='row' class='specalt'></th>").html(i+1));
				td = "<td class='alt'></td>";
			}
			
		    
		    tr.append($(td).html(items[i].employeeNum))
		      .append($(td).html(items[i].name))
		      .append($(td).html(items[i].userName))
		      .append($(td).html(items[i].roleName))
		      .append($(td).html(items[i].areaName));
            
            var lastCol = $(td);

            /*var delBtn = $("<a></a>");
            delBtn.attr("href","#");
            delBtn.html("删除");
            delBtn.attr("onclick", "delManager("+items[i].employeeId+",'"+items[i].name+"')");*/
            
            var updateBtn = $("<a></a>");
            updateBtn.attr("href","#");
            updateBtn.html("修改");
            updateBtn.attr("onclick", "jumpToUpdateManager("+items[i].employeeId+")");

            lastCol.append(updateBtn);


            tr.append(lastCol);
            //tr.attr("ondblclick", "queryRoleDetail("+items[i].roleId+")");
			$("#manager_table tbody").append(tr);
		}
		
		setPageBtnClick(datas);
		setPageInfo(datas);
	}
    function setPageInfo(datas) {
    	$("#page_info").empty();
        $("#page_info").html("共有&nbsp;" 
                + datas.totalCount + "&nbsp;条数据&nbsp;&nbsp;"
                + "每页&nbsp;" + datas.pageSize + "&nbsp;条&nbsp;当前&nbsp;"
                + (datas.currentPageIndex + 1) + "/" + datas.indexes.length + "页");
    	
    }
    function setPageBtnClick(datas) {
    	$("#page_first").unbind("click");
    	$("#page_prev").unbind("click");
    	$("#page_next").unbind("click");
    	$("#page_last").unbind("click");
    	$("#page_goto").unbind("click");
    	
        $("#page_first").click(function() {
        	findByPage(0);
        	pageObjRef.startIndex = 0;
         });
        
		$("#page_prev").click(function () {
			var startIndex = 0;
			if (datas.currentPageIndex >= 0) {
				if (datas.indexes.length > 0) {
					startIndex = datas.indexes[datas.currentPageIndex - 1];
				}
			}
			pageObjRef.startIndex = startIndex;
			findByPage(startIndex);
		});

		$("#page_next").click(function () {
			var startIndex = 0;

			if (datas.indexes.length > 0) {
				if (datas.currentPageIndex + 1 >= datas.indexes.length) {
					startIndex = datas.indexes[datas.indexes.length - 1];
				} else {
					startIndex = datas.indexes[datas.currentPageIndex + 1];
				}
				
				
			}
			pageObjRef.startIndex = startIndex;
			findByPage(startIndex);
		});

		$("#page_last").click(function () {
			var startIndex = 0;
			if (datas.indexes.length > 0) {
				startIndex = datas.indexes[datas.indexes.length - 1];
		    }
			pageObjRef.startIndex = startIndex;
			findByPage(startIndex);
		});

		$("#page_goto").click(function (){
			var startIndex = 0;
			if ($("#page_num").val() == null || $("#page_num").val().length == 0) {
				alert("请填写要跳转的页面!");
				return;
		    }
			
			if (isNaN($("#page_num").val())) {
				alert("页面必须是数字!");
				return;
		    }

			if ($("#page_num").val() <= 0 ) {
				alert("该页面不存在!");
				return;
			}

			startIndex = datas.indexes[$("#page_num").val()  - 1];
			pageObjRef.startIndex = startIndex;
			findByPage(startIndex);
			
		});
    }
	
	function pageObj() {
		this.pageSize = 5;
		this.startIndex = "${startIndex}";
	}

	var pageObjRef = new pageObj();
	</script>
</html>
